package com.punai.datapush.controller;

import com.punai.common.annotation.Log;
import com.punai.common.bos.KVBO;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.enums.BusinessType;
import com.punai.common.enums.ReleaseState;
import com.punai.common.utils.SecurityUtils;
import com.punai.datapush.domain.EcoReleaseIndicator;
import com.punai.datapush.domain.IndexZBStructItem;
import com.punai.datapush.domain.dto.IndicatorReleaseForm;
import com.punai.datapush.domain.dto.IndicatorSelectProjectForm;
import com.punai.datapush.service.IEcoReleaseIndicatorService;
import com.punai.externalccb.bo.EcoZbstructitemBo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 【请填写功能名称】Controller
 * 
 * @author zxw_
 * @date 2023-05-03
 */
@RestController
@RequestMapping("/datapush/indicator")
public class EcoReleaseIndicatorController extends BaseController
{
    @Autowired
    private IEcoReleaseIndicatorService ecoReleaseIndicatorService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('datapush:indicator:list')")
    @GetMapping("/list")
    public TableDataInfo list(EcoReleaseIndicator ecoReleaseIndicator)
    {
        startPage();
        if (ecoReleaseIndicator.getDateRange() != null && ecoReleaseIndicator.getDateRange().length >= 2) {
            String timeStart = ecoReleaseIndicator.getDateRange()[0];
            String timeEnd = ecoReleaseIndicator.getDateRange()[1];
            ecoReleaseIndicator.setTimeStart(timeStart);
            ecoReleaseIndicator.setTimeEnd(timeEnd);
        }
        List<EcoReleaseIndicator> list = ecoReleaseIndicatorService.selectEcoReleaseIndicatorList(ecoReleaseIndicator);
        return getDataTable(list);
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('datapush:indicator:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(ecoReleaseIndicatorService.selectEcoReleaseIndicatorById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('datapush:indicator:add')")
    @Log(title = "新增发布", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EcoReleaseIndicator ecoReleaseIndicator)
    {
        ecoReleaseIndicator.setReleaseDate(new Date());
        ecoReleaseIndicator.setReleaseUser(SecurityUtils.getLoginUser().getUsername());
        ecoReleaseIndicator.setReleaseState(ReleaseState.N.getType());
        return toAjax(ecoReleaseIndicatorService.insertEcoReleaseIndicator(ecoReleaseIndicator));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('datapush:indicator:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EcoReleaseIndicator ecoReleaseIndicator)
    {
        return toAjax(ecoReleaseIndicatorService.updateEcoReleaseIndicator(ecoReleaseIndicator));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('datapush:indicator:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(ecoReleaseIndicatorService.deleteEcoReleaseIndicatorByIds(ids));
    }


    @GetMapping("initSelectTableData")
    public TableDataInfo initSelectTableData(IndicatorSelectProjectForm form) {
        startPage();
        return getDataTable(ecoReleaseIndicatorService.selectTableData(form));
    }

    @GetMapping("saveSelectTableData")
    public AjaxResult saveSelectTableData(IndicatorSelectProjectForm form) {
        String stagetype = form.getStagetype();
        String proId = form.getProId();
        String pubid = form.getId();
        boolean flag = ecoReleaseIndicatorService.saveCheckedIndicator(proId, pubid, stagetype);
        if (flag) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }


    @GetMapping("savePushSelectTableData")
    public AjaxResult savePushSelectTableData(IndicatorSelectProjectForm form) {
        String stagetype = form.getStagetype();
        String proId = form.getProId();
        String pubid = form.getId();
        boolean flag = ecoReleaseIndicatorService.saveAndPublishCheckedIndicator(proId, pubid, stagetype);
        if (flag) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    @GetMapping("resetIndicator")
    public AjaxResult resetIndicator(IndicatorSelectProjectForm form) {
        String pubid = form.getId();
        String eye = form.getEye();
        if ("Y".equals(eye)) {
            EcoReleaseIndicator ecoReleaseIndicator = ecoReleaseIndicatorService.selectEcoReleaseIndicatorById(pubid);
            ecoReleaseIndicator.setReleaseState(ReleaseState.Y.getType());
            boolean flag = ecoReleaseIndicatorService.updateById(ecoReleaseIndicator);
            if (flag) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error();
            }
        }
        if ("R".equals(eye)) {
            EcoReleaseIndicator ecoReleaseIndicator = ecoReleaseIndicatorService.selectEcoReleaseIndicatorById(pubid);
            ecoReleaseIndicator.setReleaseState(ReleaseState.R.getType());
            boolean flag = ecoReleaseIndicatorService.updateById(ecoReleaseIndicator);
            if (flag) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error();
            }
        }

        EcoReleaseIndicator ecoReleaseIndicator = ecoReleaseIndicatorService.selectEcoReleaseIndicatorById(pubid);
        ecoReleaseIndicator.setReleaseState(ReleaseState.N.getType());
        boolean flag = ecoReleaseIndicatorService.updateById(ecoReleaseIndicator);
        if (flag) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    @GetMapping("indicatorShow")
    public AjaxResult indicatorShow(IndicatorReleaseForm indicatorReleaseForm) {
        String proId = indicatorReleaseForm.getProId();
        String stagetype = indicatorReleaseForm.getStagetype();
        List<KVBO> list = ecoReleaseIndicatorService.findZbListGroupBy(stagetype, proId);
        return AjaxResult.success(list);
    }

    @GetMapping("initZbTableData")
    public AjaxResult initZbTableData(IndicatorReleaseForm form) {
        List<EcoZbstructitemBo> list = ecoReleaseIndicatorService.findZbList(form.getStagetype(), form.getProId(), form.getZbid());
        return AjaxResult.success(list);
    }

    @GetMapping("getIndicatorOne")
    public AjaxResult getIndicatorOne(IndicatorReleaseForm form) {
        List<IndexZBStructItem> list = ecoReleaseIndicatorService.findListZbstructItemIndex(form);
        return AjaxResult.success(list);
    }

    @GetMapping("checkShow")
    public AjaxResult checkShow(IndicatorReleaseForm form) {
        boolean flag = ecoReleaseIndicatorService.checkShow(form);
        return AjaxResult.success(flag);
    }
}
